/**
 * @file   LoanAuthCnlService.java
 * @date   2012-11-22上午12:08:20
 * @author User
 */
package com.gzceb.zhgjj.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gzceb.zhgjj.util.SpringContextUtil;
import com.gzceb.zhgjj.util.Utils;
import com.gzceb.zhgjj.web.dao.LoanAuthDAO;
import com.gzceb.zhgjj.web.entity.LoanAuth;
import com.opensymphony.xwork2.util.logging.Logger;

/**
 * <p>贷款授权撤销服务层类。</p>
 *
 * @date   2012-11-22上午12:08:20 
 * @author liqing
 * @description 
 */
public class LoanAuthCnlService {

	private LoanAuthDAO loanAuthDAO;
	
	public LoanAuthCnlService(){
		
	}

	public LoanAuthDAO getLoanAuthDAO() {
		return loanAuthDAO;
	}

	public void setLoanAuthDAO(LoanAuthDAO loanAuthDAO) {
		this.loanAuthDAO = loanAuthDAO;
	}
	
	/**
	 * 判断是否贷款撤销的记录已经存在于数据库表 info_loanauth 中，
	 * 需判断 记录的撤销标志是否置位
	 * @param contCode String
	 * @return boolean
	 * 
	 */
	public boolean isExist(String contCode){
		LoanAuth loanAuth = new LoanAuth();
		loanAuth = loanAuthDAO.findByContCode(contCode);
		if(null != loanAuth && "1".equals(loanAuth.getRevokedtype())){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断是否有贷款审批记录存在与库表 info_loanauth 中，
	 * 不需判断记录的撤销标志
	 * @param contCode String
	 * @return boolean
	 */
	public boolean isLoanExist(String contCode){
		LoanAuth loanAuth = new LoanAuth();
		loanAuth = loanAuthDAO.findByContCode(contCode);
		if(null != loanAuth ){
			return true;
		}
		return false;
	}
	
	/**
	 * 保存贷款授权撤销信息到贷款授权表中
	 */
	public boolean saveToDB(LoanAuth loanAuth){
		boolean isSuc = true;
		if(isLoanExist(loanAuth.getContcode())){
			try{
				loanAuthDAO.attachDirty(loanAuth);
			}catch(RuntimeException re){
				isSuc = false;
			}
			
			
		}else{
			isSuc = false;
		}
		
		return isSuc;
	}
	
	
	public static void main(String[] args){
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("/spring/applicationContext.xml");
		
		LoanAuthCnlService lacs = (LoanAuthCnlService) SpringContextUtil.getBean("loanAuthCnlService");
		
		LoanAuth loanAuth = new LoanAuth();
		loanAuth.setContcode("12345");
		loanAuth.setIsrevoked("1");
		loanAuth.setRevokeddate("20121218");
		loanAuth.setRevokedtype("2");
		loanAuth.setRvkinpdate(Utils.getCurrDateStr19Str());
		
		lacs.saveToDB(loanAuth);
	}
}
